package housing.task1.actor;

import housing.task1.document.Request;
import housing.task1.document.WorkPlan;
import housing.task1.generator.AutoCompleter;

import java.util.GregorianCalendar;

/**
 * @author Marshall
 */
public class Worker extends Human {
    private AutoCompleter.WorkType specialty;
    private boolean isAssigned;
    private GregorianCalendar workBegin;
    private GregorianCalendar workEnd;
    private Request task = null;


    public Worker() {
        this(AutoCompleter.WorkType.generateWorkType());
    }

    public Worker(AutoCompleter.WorkType spec) {
        super();
        specialty = spec;
        setWorkBegin(new GregorianCalendar(1900, 01, 01));
        setWorkEnd(new GregorianCalendar(1900, 01, 01));
        addToWorkPlan();
    }//Worker()


    /**
     *
     */
    protected void addToWorkPlan() {
        WorkPlan.getInstance().getWorkers().add(this);

    }//void AddToWorkPlan()

    public boolean isIddle(GregorianCalendar d) {
        return (d.before(workBegin) || d.after(workEnd));
    }//boolean isIddle()

    public void assignToWork(Request request) {
        GregorianCalendar begin = request.getDate();
        GregorianCalendar end = AutoCompleter.WorkScale.setWorkEndDate(begin, request.getScale());
        setWorkBegin(begin);
        setWorkEnd(end);
        setTask(request);
        setAssigned(true);
    }//void assignToWork()

//    public void assignToWork(GregorianCalendar b, GregorianCalendar e) {
//        workBegin = b;
//        workEnd = e;
//        if (e.after(new GregorianCalendar(1900, 01, 01))) {
//            setAssigned(true);
//        } else {
//            setAssigned(false);
//        }
//    }//void assignToWork()

    @Override
    public String toString() {
        return ("Worker:\t" + this.get_firstName() +
                "\tSpec: " + this.getSpecialty() +
                "\tIs assigned: " + this.isAssigned());
    }

    @Override
    public void act() {
        String status = isAssigned() ? " is working on the job " + getTask().getSender() + " " + getTask().getWork().toString()
                                     : " is iddle";
        System.out.println("Worker " + get_firstName() + status);
    }

    //=============================================================================

    /**
     * @param specialty the specialty to set
     */
    public void setSpecialty(AutoCompleter.WorkType specialty) {
        this.specialty = specialty;
    }

    /**
     * @return the specialty
     */
    public AutoCompleter.WorkType getSpecialty() {
        return specialty;
    }

    /**
     * @param workBegin the workBegin to set
     */
    public void setWorkBegin(GregorianCalendar workBegin) {
        this.workBegin = workBegin;
    }

    /**
     * @return the workBegin
     */
    public GregorianCalendar getWorkBegin() {
        return workBegin;
    }

    /**
     * @param workEnd the workEnd to set
     */
    public void setWorkEnd(GregorianCalendar workEnd) {
        this.workEnd = workEnd;
    }

    /**
     * @return the workEnd
     */
    public GregorianCalendar getWorkEnd() {
        return workEnd;
    }

    public void setAssigned(boolean isAssigned) {
        this.isAssigned = isAssigned;
    }

    public boolean isAssigned() {
        return isAssigned;
    }

    public Request getTask() {
        return task;
    }

    public void setTask(Request task) {
        this.task = task;
    }
}//class Worker

